-- attrib_improve
-- Created by chengb Jul/15/2015
-- 成就：属性提升

-- 内部函数声明
local checkComplete;
local isImproveByBuff;
local whenAttribBonus;
local whenRefreshProp;

-- 注册玩家获得属性的回调
EventMgr.register("achievements/attrib_improve", event.BONUS_ATTRIB, function(args)
    whenAttribBonus(args);
end);

-- 关注玩家属性刷新的回调
EventMgr.register("achievements/attrib_improve", event.RERESH_PROP, function(args)
    whenRefreshProp(args);
end);

-- 注册玩家获得属性的回调
whenAttribBonus = function(args)
    local attrib = args.attrib;
    
    checkComplete(attrib);
end

-- 属性刷新的事件
whenRefreshProp = function(args)
    local who = args.who;
    
    if who.type ~= OBJECT_TYPE_USER then
        -- 不是玩家
        return;
    end
    
    local careList = { "attack", "magic", "max_hp", "max_mp", };
    for _, attrib in pairs(careList) do
        checkComplete(attrib);
    end
end

-- 是否通过buff提升了属性
isImproveByBuff = function(attrib)
    local buffList = PropM.getBuff(ME.user);
    
    if #buffList == 0 then
        return false;
    end

    for _, arr in pairs(buffList) do
        if arr[2] == attrib then
            return true;
        end
    end
         
    return false;
end

-- 检查成就完成 
checkComplete = function(attrib)
    -- 仅关注：攻击、魔力、最大生命值、最大魔法值
    local careList = { "attack", "magic", "max_hp", "max_mp", };
    if table.indexOf(careList, attrib) == -1 then
        return;
    end
    
    -- 不在迷宫中或者尚未正常进行迷宫，无视
    if not DungeonM.isInDungeon() or
       not DungeonM.getDungeonId() or
       DungeonM.getState() ~= DUNGEON_GAMING then
        return;
    end

    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();

    local layer = DungeonM.currentLayer();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("attrib_improve");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
            -- 不是该迷宫的成就
            return false;
        end

        -- 判断是否是需求属性
        local config = AchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["attrib"]) == 'string' and 
                ruleArg["attrib"] ~= attrib then
                -- 属性不匹配
                return false;
            end

            if ME.user:queryAttrib(attrib) < ruleArg["value"] then
                -- 属性值不达标
                return false;
            end
            
            -- 如果是通过buff提升的，不算
            if isImproveByBuff(attrib) then
                return false;
            end
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {
    -- 获取成就描述
    queryDesc = function(id)
        local config = AchievementM.query(id);

        local ruleArg = config["rule_arg"];
        local value   = ruleArg["value"];
        local desc    = config["desc"];

        desc = string.gsub(desc, "{0}", value);

        return desc;
    end,
};